package cn.gson.crm.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.gson.crm.model.Jdbc;
import cn.gson.crm.model.entity.Position;


public class PositionDao {
	// 页大小
	private int pageSize = 7;
	
	//根据职位状态找到职位所有信息
	public List<Position> getPostByStatus(Integer postStatus) throws SQLException{
		String sql = "select * from position where post_status = ? order by post_id   desc ";
		List<Map<String,Object>> listData = Jdbc.getInstace().executeQueryForList(sql,postStatus);
		List<Position> postList = null;
		if(listData != null && listData.size()>0){
			postList = new ArrayList<>();
			for (Map<String, Object> map : listData) {
				postList.add(new Position(
						Integer.parseInt(map.get("post_id").toString()),
						(String)map.get("post_name"),
						(String)map.get("post_describe"),
						Integer.parseInt(map.get("post_status").toString())
						));
			}
 		}
		return postList;
	}	
	//获得单个职位信息
	public Position getPost(String pId) throws SQLException{
		String sql = "select * from position where post_id = ?";
		Map<String,Object> data = Jdbc.getInstace().executeQuery(sql,pId);
 		Position post = null;
 		if(data != null){
 			post = buildPost(data);
 		}
		return post;	
	}
	
	//保存职位
	public boolean savePost(Position post) throws SQLException{
		if(post.getPostId() != null){
			String  sql = "update position set post_name = ?,post_describe = ? where post_id = ?";
			return Jdbc.getInstace().executeUpdate(sql, post.getPostName(),post.getPostDescribe(),post.getPostId());
		}else{
			String sql = "INSERT into position (post_name,post_describe) values (?,?)";
			return Jdbc.getInstace().executeUpdate(sql, post.getPostName(),post.getPostDescribe());
		}
	}
	
	//删除职位（物理删除，改变状态）
	public boolean deletePost(Position post) throws SQLException{
		String sql;
		if(post.getPostStatus() == 1){
			sql = "update position set post_status = '0' where post_id = ?";
		
		}else{
			sql = "update position set post_status = '1' where post_id = ?";
		}
		return Jdbc.getInstace().executeUpdate(sql,post.getPostId());
		
	}
	
	/**
	 * 查询分页数据
	 * 
	 * @param type
	 * @param page
	 * @return
	 * @throws SQLException
	 */
	public List<Position> getPagePostListByType(int postStatus,int page) throws SQLException {
		int start = (page - 1) * pageSize;
		String sql = "select * from position where post_status = ? order by post_id desc limit ?,?";
		return queryUserList(sql,postStatus,start, pageSize);
	}
	
	/**
	 * 获取总页数
	 * 
	 * @param type
	 * @return
	 * @throws SQLException
	 */
	public int getTotalPage(int postStatus) throws SQLException {
		String sql = "select count(*) total from position where post_status = ?";
		Map<String, Object> map = Jdbc.getInstace().executeQuery(sql,postStatus);
		Integer total = Integer.parseInt(map.get("total").toString());
		return (int) Math.ceil(total.doubleValue() / pageSize);
	}

	private List<Position> queryUserList(String sql, Object... params) throws SQLException {
		List<Map<String, Object>> listData = Jdbc.getInstace().executeQueryForList(sql, params);
		List<Position> userList = null;
		if (listData != null && listData.size() > 0) {
			userList = new ArrayList<>();
			for (Map<String, Object> data : listData) {
				userList.add(buildPost(data));
			}
		}
		return userList;
	}
	
	private Position buildPost(Map<String,Object> data){
			Position post = new Position();		
 			post.setPostId(Integer.parseInt(data.get("post_id").toString()));
 			post.setPostName((String)data.get("post_name"));
 			post.setPostDescribe((String)data.get("post_describe"));				
 			post.setPostStatus(Integer.parseInt(data.get("post_status").toString()));
		return post;
	}
	
}
